/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  RoundImageView.java   
 * @Package com.jiyi.widget   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-20 下午4:22:51   
 * @version V1.0     
 */
package com.jiyi.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.swift.maintenance.R;


public class RoundImageView extends android.support.v7.widget.AppCompatImageView {

	//是否圆角
	private boolean isRound = false;
	//圆角的角度
	private int angle = 0;
	//圆角的类型 0 全部圆角 1 top 2 right 3 bottom 4 left
	private int roundType = 0;
	//是否蒙板
	private boolean isMask = false;
	//蒙板的颜色
	private int alphaColor = 0;

	public RoundImageView(Context context) {
		super(context);
	}

	public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public RoundImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.RoundImageView);
		isRound = a.getBoolean(R.styleable.RoundImageView_isRound, false);
		angle = a.getInteger(R.styleable.RoundImageView_angle, 0);
		roundType = a.getInteger(R.styleable.RoundImageView_roundType, 0);
		isMask = a.getBoolean(R.styleable.RoundImageView_isMask, false);
		alphaColor = a.getColor(R.styleable.RoundImageView_alpha_riv, Color.parseColor("#20000000"));
		a.recycle();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (isRound) {
			Path path = new Path();
			RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
			float[] radii;
			switch (roundType) {
			case 0:
				//全部圆角
				path.addRoundRect(rect, angle, angle, Path.Direction.CW);
				break;
			case 1:
				//上边圆角	数组的意思是 上左	--	上右	--	下右	--	下左
				radii = new float[] { angle, angle, angle, angle, 0, 0, 0, 0 };
				path.addRoundRect(rect, radii, Path.Direction.CW);
				break;
			case 2:
				//右边圆角
				radii = new float[] { 0, 0, angle, angle, angle, angle, 0, 0 };
				path.addRoundRect(rect, radii, Path.Direction.CW);
				break;
			case 3:
				//下边圆角
				radii = new float[] { 0, 0, 0, 0, angle, angle, angle, angle };
				path.addRoundRect(rect, radii, Path.Direction.CW);
				break;
			case 4:
				//左边圆角
				radii = new float[] { angle, angle, 0, 0, 0, 0, angle, angle };
				path.addRoundRect(rect, radii, Path.Direction.CW);
				break;
			default:
				break;
			}
			
			canvas.clipPath(path);
		}
		super.onDraw(canvas);
		if (isMask) {
			//给图片添加蒙板效果
			canvas.drawColor(alphaColor);
		}
		
	}
}
